package com.questiono;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

public class FBConnect extends Activity {

	   @SuppressWarnings("deprecation")
		private AsyncFacebookRunner mAsyncRunner;
	   String FILENAME = "AndroidSSO_data";
	   private SharedPreferences mPrefs;
	   String TAG_ERROR="error";
		
	
		public void loginToFacebook(final Facebook facebook) {
			 
			
			Log.d(TAG_ERROR,facebook.toString());
		    mAsyncRunner = new AsyncFacebookRunner(facebook);
		    mPrefs = getPreferences(MODE_PRIVATE);
		    String access_token = mPrefs.getString("access_token", null);
		    long expires = mPrefs.getLong("access_expires", 0);
		 
		    if (access_token != null) {
		        facebook.setAccessToken(access_token);
		    }
		 
		    if (expires != 0) {
		        facebook.setAccessExpires(expires);
		    }
		 
		    if (!facebook.isSessionValid()) {
		        facebook.authorize(this,
		                new String[] { "email", "publish_stream" },
		                new DialogListener() {
		 
		                    @Override
		                    public void onCancel() {
		                        // Function to handle cancel event
		                    }
		 
		                    @Override
		                    public void onComplete(Bundle values) {
		                        // Function to handle complete event
		                        // Edit Preferences and update facebook acess_token
		                        SharedPreferences.Editor editor = mPrefs.edit();
		                        editor.putString("access_token",
		                                facebook.getAccessToken());
		                        editor.putLong("access_expires",
		                                facebook.getAccessExpires());
		                        editor.commit();
		                    }
		 
		                    @Override
		                    public void onError(DialogError error) {
		                        // Function to handle error
		 
		                    }
		 
		                    @Override
		                    public void onFacebookError(FacebookError fberror) {
		                        // Function to handle Facebook errors
		 
		                    }
		 
		                });
		    }
		}
		
		public void postToWall(final Facebook facebook) {
		    // post on user's wall.
		    facebook.dialog(this, "feed", new DialogListener() {
		 
		        @Override
		        public void onFacebookError(FacebookError e) {
		        }
		 
		        @Override
		        public void onError(DialogError e) {
		        }
		 
		        @Override
		        public void onComplete(Bundle values) {
		        }
		 
		        @Override
		        public void onCancel() {
		        }
		    });
		 
		}

}
